const gulp = require('gulp');
const less = require('gulp-less');
const autoprefixer = require('less-plugin-autoprefix');
const cleanCSS = require('gulp-clean-css');
const path = require('path');
const htmlImport = require('gulp-html-import');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const sourcemaps = require('gulp-sourcemaps');
const changed = require('gulp-changed');

gulp.task('less', function () {
    gulp.src('src/css/*.less')
        .pipe(sourcemaps.init())
        .pipe(less({
            plugins: [
                new autoprefixer({
                    browsers: ['Android >= 3', 'iOS >= 7'],
                }),
            ],
        }))
        .pipe(sourcemaps.write('./maps/'))
        .pipe(gulp.dest(path.resolve(__dirname, './fxjapp/css/')))
        .pipe(gulp.dest(`D:\\WangShunPing\\Code\\HybridApp\\分享家短租\\css`));
});

gulp.task('babel', function () {
    gulp.src('src/script/*.js')
        .pipe(changed(path.resolve(__dirname, './fxjapp/script/')))
        .pipe(sourcemaps.init())
        .pipe(babel({
            presets: ['env'],
        }))
        .pipe(sourcemaps.write('./maps/'))
        .pipe(gulp.dest(path.resolve(__dirname, './fxjapp/script/')))
        .pipe(gulp.dest(`D:\\WangShunPing\\Code\\HybridApp\\分享家短租\\script`));
});


gulp.task('import', function () {
    gulp.src('src/html/*.html')
        .pipe(htmlImport('./components/'))
        .pipe(gulp.dest(path.resolve(__dirname, './fxjapp/html/')))
        .pipe(gulp.dest(`D:\\WangShunPing\\Code\\HybridApp\\分享家短租\\html`));
});

gulp.task('createjs', () => {
    gulp.src('src/script/*.js')
        .pipe(sourcemaps.init())
        .pipe(babel({
            presets: ['env'],
        }))
        .pipe(sourcemaps.write('./maps/'))
        .pipe(gulp.dest(path.resolve(__dirname, './fxjapp/script/')))
        .pipe(gulp.dest(`D:\\WangShunPing\\Code\\HybridApp\\分享家短租\\script`));
});

gulp.task('default', function () {
    gulp.watch('src/**/*.less', event => {
        gulp.src(path.relative(__dirname, event.path))
            .pipe(sourcemaps.init())
            .pipe(less({
                plugins: [
                    new autoprefixer({
                        browsers: ['Android >= 3', 'iOS >= 7'],
                    }),
                ],
            }))
            .pipe(sourcemaps.write('./maps/'))
            .pipe(gulp.dest(path.resolve(__dirname, './fxjapp/css/')))
            .pipe(gulp.dest(`D:\\WangShunPing\\Code\\HybridApp\\分享家短租\\css`));
    });
    gulp.watch('src/**/*.js', ['babel']);
    gulp.watch('src/**/*.html', event => {
        gulp.src(path.relative(__dirname, event.path))
            .pipe(htmlImport('./components/'))
            .pipe(gulp.dest(path.resolve(__dirname, './fxjapp/html/')))
            .pipe(gulp.dest(`D:\\WangShunPing\\Code\\HybridApp\\分享家短租\\html`));
    });
    gulp.watch('./components/*.html', ['import']);
});